<template>
  <div>
      <el-card>
          <el-input placeholder="请输入账号查询用户" v-model="selectinput" class="input-with-select" clearable @clear="getPusers()">
            <el-button slot="append" icon="el-icon-search" @click="selectuser()"></el-button>
          </el-input>
          <el-button type="primary" style="margin-bottom:10px; float:right" @click="addDialogVisible=true">添加用户</el-button>
        <el-table :data="userlist" border style="width: 100%" :row-class-name="tableRowClassName" stripe>
            <el-table-column prop="useraccount" label="账号" width="auto" fixed></el-table-column>
            <el-table-column prop="userrname" label="姓名" min-width="150"></el-table-column>
            <el-table-column prop="userid" label="身份证号" min-width="170"></el-table-column>
            <el-table-column prop="usernumber" label="联系电话" min-width="120"></el-table-column>
            <el-table-column prop="userwallet" label="钱包" width="auto"> </el-table-column>
            <el-table-column label="查看订单" min-width="110">
                <template slot-scope="scope">
                    <el-button @click="getorder(scope.row.userid)">查看</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="70">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.state" disabled active-value="true"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="145px" fixed="right">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="showEditDialog(scope.row.id)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="deleteuser(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="this.pagenum"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="this.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </el-card>

      <!-- 添加用户对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClosed">
      <!-- 内容主体区 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="账号" prop="useraccount">
          <el-input v-model="addForm.useraccount"></el-input>
        </el-form-item>
        <el-form-item label="名字" prop="userrname">
          <el-input v-model="addForm.userrname"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="userid">
          <el-input v-model="addForm.userid"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="usernumber">
          <el-input v-model="addForm.usernumber"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userpassword">
          <el-input v-model="addForm.userpassword"></el-input>
        </el-form-item>
        <el-form-item label="钱包余额" prop="userwallet">
          <el-input v-model="addForm.userwallet"></el-input>
        </el-form-item>
        <el-form-item label="个性签名" prop="usersignature">
          <el-input v-model="addForm.usersignature"></el-input>
        </el-form-item>
        <el-form-item label="账号状态">
          <template>
              <el-switch v-model="value" @change="userStateChange()" active-color="#13ce66" inactive-color="#ff4949" active-value="true" inactive-value="false"></el-switch>
          </template>
        </el-form-item>
      </el-form>
        <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserInfo">确 定</el-button>
      </span>
    </el-dialog>
       <!-- 编辑用户对话框 -->
        <el-dialog
        title="编辑用户"
        :visible.sync="editDialogVisible"
        width="50%" @close="editDialogClosed">
        <!-- 内容主体区 -->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
            <el-form-item label="账号" prop="useraccount">
            <el-input v-model="editForm.useraccount"></el-input>
            </el-form-item>
            <el-form-item label="名字" prop="userrname">
            <el-input v-model="editForm.userrname"></el-input>
            </el-form-item>
            <el-form-item label="身份证号" prop="userid">
            <el-input v-model="editForm.userid"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="usernumber">
            <el-input v-model="editForm.usernumber"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="userpassword">
            <el-input v-model="editForm.userpassword"></el-input>
            </el-form-item>
            <el-form-item label="钱包余额" prop="userwallet">
            <el-input v-model="editForm.userwallet"></el-input>
            </el-form-item>
            <el-form-item label="个性签名" prop="usersignature">
            <el-input v-model="editForm.usersignature"></el-input>
            </el-form-item>
            <el-form-item label="账号状态">
            <template>
                <el-switch v-model="value" @change="userStateChange()" active-color="#13ce66" inactive-color="#ff4949" active-value="true" inactive-value="false" active-text="不操作不做更改"></el-switch>
            </template>
            </el-form-item>
        </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>

      <!-- 订单查询页面 -->
      <el-dialog title="查询订单"
        :visible.sync="getDialogVisible"
        width="90%" @close="editDialogClosed">
        <!-- 内容主体区 -->
          <el-card style="margin-top:0px">
            <el-table :data="orderlist" border style="width: 100%" :row-class-name="tableRowClassName" stripe>
              <el-table-column prop="ordernumber" label="订单号" width="auto" fixed></el-table-column>
              <el-table-column prop="ordertime" label="下单时间" min-width="190"></el-table-column>
              <el-table-column prop="orderynumber" label="预留手机号" min-width="120"></el-table-column>
              <el-table-column prop="orderusetimestart" label="使用开始时间" min-width="190"></el-table-column>
              <el-table-column prop="orderusetimeend" label="使用结束时间" width="190"> </el-table-column>
              <el-table-column prop="orderunit" label="租用管理单位编号" width="150"> </el-table-column>
              <el-table-column prop="orderfacilities" label="租用设施类型" width="110"> </el-table-column>
              <el-table-column prop="ordervenue" label="租用设施编号" width="110"> </el-table-column>
              <el-table-column prop="orderstatus" label="处理状态" width="auto"> </el-table-column>
              <el-table-column prop="ordermoney" label="订单金额" width="auto"> </el-table-column>
              <el-table-column prop="orderid" label="下单用户身份证号码" width="180"> </el-table-column>
              <el-table-column prop="state" label="完成状态" width="auto"> </el-table-column>
            </el-table>
            <el-pagination
              @size-change="handleSizeChange1"
              @current-change="handleCurrentChange1"
              :current-page="this.pagenum1"
              :page-sizes="[5, 10, 20, 50]"
              :page-size="this.pagesize1"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total1">
            </el-pagination>
            <el-button type="primary" style="margin-top:10px" @click="getDialogVisible=false">关闭</el-button>
          </el-card>
      </el-dialog>
  </div>
</template>

<script>
export default {

    data () {

        // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        // 合法手机号
        return cb()
      }
      cb(new Error('请输入合法手机号'))
    }

      return {

          user: null,

          newstate:'',

            // 当前的页数
            pagenum:1,
            // 当前每页显示多少条数据
            pagesize:10,
            // 普通用户等级
            query:'0',

            userlist: [],
            total: 0,


            // 当前的页数
            pagenum1:1,
            // 当前每页显示多少条数据
            pagesize1:10,
            // 普通用户等级
            query1:'0',
            total1: 0,


            addDialogVisible:false,

            // 添加用户的表单数据
        addForm: {
            useraccount: '',
            userrname: '',
            userid: '',
            usernumber: '',
            userpassword: '',
            userwallet: '',
            usersignature: '',
            completedorder: '',
            pendingorders: '',
            state: 'true'

        },
        value:'',
        // 添加表单的验证规则对象
        addFormRules: {
            useraccount: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 10, message: '账号的长度在3~10个字符之间', trigger: 'blur' }
            ],
            userrname: [
            { required: true, message: '请输入名字', trigger: 'blur' },
            { min: 1, max: 20, message: '名字的长度在1~20个字符之间', trigger: 'blur' }
            ],
            userid: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { min: 18, max: 18, message: '身份证号的长度为18位', trigger: 'blur' }
            ],
            usernumber: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '手机号的长度在11个字符', trigger: 'blur' },
            { validator: checkMobile, trigger: 'blur' }
            ],
            userpassword: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 16, message: '手机号的长度在3~16个字符之间', trigger: 'blur' }
            ]
        },

         // 控制修改用户对话框的显示与隐藏
        editDialogVisible: false,
        // 查询到用户信息对象
        editForm: {},
        // 修改表单的验证规则对象
        editFormRules: {
            useraccount: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 10, message: '账号的长度在3~10个字符之间', trigger: 'blur' }
            ],
            userrname: [
            { required: true, message: '请输入名字', trigger: 'blur' },
            { min: 1, max: 20, message: '名字的长度在1~20个字符之间', trigger: 'blur' }
            ],
            userid: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
            { min: 18, max: 18, message: '身份证号的长度为18位', trigger: 'blur' }
            ],
            usernumber: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '手机号的长度在11个字符', trigger: 'blur' },
            { validator: checkMobile, trigger: 'blur' }
            ]
        },

        getDialogVisible:false,

        orderlist:[],

        selectinput:''
        
      }
    },
    created() {
        this.getPusers()

    },
    methods: {

        //获取普通用户列表?pagenum=1&pagesize=5&query=0
        getPusers () {
            
            this.getRequest('/users/?pagenum='+this.pagenum+'&pagesize='+this.pagesize+'&query='+this.query).then(resp => {
                this.userlist = resp.data.current_data
                this.total = resp.data.total
            })
        },

        //删除用户
        deleteuser (id) {
            this.$confirm('此操作将永久删除该账号, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {

            this.deleteRequest('/deleteaccount/'+id).then(resp => {
                this.getPusers()
            }) 
            }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });          
            });
            
        },
        //账号状态
        userStateChange() {
            this.addForm.state=this.value
            this.editForm.state=this.value
        },

        //添加普通用户
        addUserInfo() {
            this.$refs.addFormRef.validate(valid => {
                if (valid) {
                    this.postRequest('/user',this.addForm).then(resp => {
                        if (resp.code==201) {
                        this.getPusers()
                        this.addDialogVisible=false
                        }
                        return false
                    })
                } else {
                    this.$message.error('请输入必要信息！')
                    return false
                }
            })
        },


        //修改普通用户
        showEditDialog(id) {
            this.getRequest('/user/'+id).then(resp => {
                // console.log(resp)
                this.editForm=resp.data
                this.editDialogVisible=true
            })
        },

        
        //确定修改普通用户
        editUserInfo() {
            this.$refs.editFormRef.validate(valid => {
                if (valid) {
                    this.putRequest('/updata',this.editForm).then(resp => {
                        this.getPusers()
                        this.editDialogVisible=false
                        
                    })
                } else {
                    this.$message.error('请输入必要信息！')
                    return false
                }
            })
        },

        //查看订单
        getorder(id) {
            this.getDialogVisible=true
            this.getRequest('/order/orders?pagenum='+this.pagenum1+'&pagesize='+this.pagesize+'&query='+id).then(resp => {
              this.orderlist=resp.data.current_data
              this.total1 = resp.data.total
              // console.log(resp)
            })
        },

        //根据账号查询用户
        selectuser() {
            if (this.selectinput.length=='0') {
              this.$message.error('请输入账号开始查询！')
              
            return false
            } else {
                this.getRequest('/userr/'+this.selectinput).then(resp => {
                this.userlist=resp.data
              })
            }
            
        },



        // 监听 pagesize 改变的事件
        handleSizeChange(newSize) {
        // console.log(newSize)
        this.pagesize = newSize
        this.getPusers()
        },
        // 监听 页码值 改变的事件
        handleCurrentChange(newPage) {
        //   console.log(newPage)
        this.pagenum = newPage
        this.getPusers()
        },


        // 监听 pagesize1 改变的事件
        handleSizeChange1(newSize) {
        // console.log(newSize)
        this.pagesize1 = newSize

        },
        // 监听 页码值 改变的事件
        handleCurrentChange1(newPage) {
        //   console.log(newPage)
        this.pagenum1 = newPage
        
        },

        // 监听添加用户对话框的关闭事件
        addDialogClosed() {
        this.$refs.addFormRef.resetFields()
        },
        // 监听修改用户对话框的关闭事件
        editDialogClosed() {
          this.$refs.editFormRef.resetFields()
        }
    }
}
</script>

<style lang="less" scoped>
.el-card {
    margin-top:20px;
}

.input-with-select {
    width: 40%;
}
</style>>
